package forum.command.console;

import forum.command.Command;
import forum.command.CommandContext;
import forum.entities.Post;
import forum.entities.Topic;
import forum.services.UserService;

import java.math.BigInteger;
import java.util.List;

public class OpenTopicCommand implements Command {

    private Topic topic;
    private BigInteger topicId;

    private UserService userService = new UserService();


    @Override
    public void execute() {
        topic = userService.getTopic(topicId);
        if (topic == null) {
            throw new IllegalArgumentException("Topic with id "  + topicId + " doesn't exist");
        }
        System.out.println("Topic name: " + topic.getTopicName());
        System.out.println("Posts :");
        List<Post> posts = userService.getPosts(topic);
        for(Post post : posts) {
            System.out.println("User " + userService.getAuthor(post.getAuthorId()).getNickName() + " wrote ");
            System.out.println("id (" + post.getId()+ ") " + post.getPostContains());
            System.out.println();
        }

    }

    @Override
    public void setParameters(String... args) {
        topicId = new BigInteger(args[0]);
    }

    @Override
    public void setContext(CommandContext context) {
    }

    @Override
    public void updateContext(CommandContext context) {
        context.setTopic(topic);
    }

    @Override
    public String help() {
        return "Type 'open_topic | %topicId%'";
    }
}
